package com.xiaoxiao.date;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private DatePicker datePicker;
    private TextView daysTextView;
    private TextView messageTextView;
    private TextView messageTextView2;
    private TextView messageTextView3;
    private TextView messageTextView4;
    private Button calculateButton;
    private ProgressBar progressBar; // 修改: 使用ProgressBar代替TextView
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datePicker = findViewById(R.id.datePicker);
        daysTextView = findViewById(R.id.daysTextView);
        messageTextView = findViewById(R.id.messageTextView);
        messageTextView2 = findViewById(R.id.messageTextView2);
        messageTextView3 = findViewById(R.id.messageTextView3);
        messageTextView4 = findViewById(R.id.messageTextView4);
        calculateButton = findViewById(R.id.calculateButton);
        progressBar = findViewById(R.id.progressBar); // 修改: 初始化ProgressBar
        calculateButton.setVisibility(View.GONE);
        messageTextView.setVisibility(View.GONE);
        messageTextView2.setVisibility(View.GONE);
        messageTextView3.setVisibility(View.GONE);
        messageTextView4.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE); // 修改: 初始化ProgressBar为不可见

        datePicker.init(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                calculateDays(year, monthOfYear, dayOfMonth);
            }
        });

        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE); // 修改: 显示ProgressBar
                calculateButton.setVisibility(View.GONE); // 隐藏计算按钮
                Toast.makeText(MainActivity.this, "LG正在玩命计算中！！！！！！", Toast.LENGTH_SHORT).show(); // 添加: 显示Toast提示
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth();
                        int day = datePicker.getDayOfMonth();
                        openResultActivity(year, month, day);
                    }
                }, 3000); // 5秒后执行跳转操作
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 重置UI状态
        progressBar.setVisibility(View.GONE);
        calculateButton.setVisibility(View.GONE);
        messageTextView.setVisibility(View.GONE);
        messageTextView2.setVisibility(View.GONE);
        messageTextView3.setVisibility(View.GONE);
        messageTextView4.setVisibility(View.GONE);
    }

    private void calculateDays(int year, int month, int day) {
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(year, month, day);
        Calendar today = Calendar.getInstance();

        long diff = today.getTimeInMillis() - selectedDate.getTimeInMillis();
        long days = diff / (24 * 60 * 60 * 1000);

        daysTextView.setText("选择的时间距离今天共计 " + days + " 天\nit is mean our together time is "+days+" days ");
        daysTextView.setTextColor(getResources().getColor(android.R.color.black));
        daysTextView.setTextSize(18);

        messageTextView.postDelayed(new Runnable() {
            @Override
            public void run() {
                messageTextView.setVisibility(View.VISIBLE);
                messageTextView.setText("是不是看起来跟1000天很近\n那你这人算一下我们在一起第1000天是几月几号");
                messageTextView.setTextColor(getResources().getColor(android.R.color.black));
                messageTextView.setTextSize(18);
            }
        }, 10000);

        messageTextView2.postDelayed(new Runnable() {
            @Override
            public void run() {
                messageTextView2.setVisibility(View.VISIBLE);
                messageTextView2.setTextColor(getResources().getColor(android.R.color.black));
                messageTextView2.setTextSize(18);
            }
        }, 24000);

        messageTextView3.postDelayed(new Runnable() {
            @Override
            public void run() {
                messageTextView3.setVisibility(View.VISIBLE);
                messageTextView3.setTextColor(getResources().getColor(android.R.color.black));
                messageTextView3.setTextSize(18);
            }
        }, 27000);

        messageTextView4.postDelayed(new Runnable() {
            @Override
            public void run() {
                messageTextView4.setVisibility(View.VISIBLE);
                messageTextView4.setTextColor(getResources().getColor(android.R.color.black));
                messageTextView4.setTextSize(18);
                calculateButton.setVisibility(View.VISIBLE);
            }
        }, 30000);
    }

    private void openResultActivity(int year, int month, int day) {
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(year, month, day);
        selectedDate.add(Calendar.DAY_OF_YEAR, 1000);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA);
        String resultDate = sdf.format(selectedDate.getTime());

        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("resultDate", resultDate);
        startActivityForResult(intent, 1); // 修改: 使用startActivityForResult启动ResultActivity
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                progressBar.setVisibility(View.GONE); // 修改: 隐藏ProgressBar
                calculateButton.setVisibility(View.VISIBLE); // 修改: 显示计算按钮
            }
        }
    }
}